<?php
if ( IPB_THIS_SCRIPT != 'admin' )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'init.php'.";
	exit();
}

class ad_upgrade
{
	var $ipsclass;
	var $failed;
	var $currect_ver;
	var $currect_name;
	var $upgrade_versions = array();

	function auto_run()
	{
		if (LIMIT_TO_ONLY_ROOT && $this->ipsclass->member['mgroup'] != $this->ipsclass->vars['admin_group'])
		{
			$this->ipsclass->admin->error("Sorry, this page is only for root admin only.");
		}
		
		$live = 1;
		@call_user_func(array(&$this->ipsclass->market_admin,"me".'h_t'."o_m".'eh')) !== true ? die() : $live;
		
		if(file_exists(ROOT_PATH."sources/null") || file_exists(ROOT_PATH."dgt_released.nfo") || file_exists(ROOT_PATH."sources/file_id.diz"))
		{
			$this->ipsclass->admin->error("The author does not like you to use his modification on nulled boards");
		}
		
		$this->failed = $this->ipsclass->market_admin->check_conditions();
		
		$version_info = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_versions','order'=>'version_id DESC','limit'=>array(1)));
		$this->currect_name = $version_info['version_name'];
		$this->currect_ver = $version_info['version_id'];
		
		$directories = $this->ipsclass->market_class->get_dir_list(ROOT_PATH."sources/market/upgrade",0);
		$version_id = 0;
		$version_name = '';
		foreach($directories as $dir)
		{
			if(strstr($dir,"up_") !== $dir || !intval(substr($dir,3)))
			{
				continue;
			}
			include(ROOT_PATH."sources/market/upgrade/".$dir."/upgrade_info.php");
			if($version_id <= $this->currect_ver)
			{
				continue;
			}
			$this->upgrade_versions[$version_id] = $version_name; 
		}
		ksort($this->upgrade_versions);
		
		if(($call = 'file_'.'exists') && ($call(constant('ROOT_PATH')."so"."urces/null") || $call(constant('ROOT_PATH')."dgt_rele"."ased.nfo") || $call(constant('ROOT_PATH')."sourc"."es/fi"."le_id.diz")))
		{
			$this->ipsclass->admin->redirect_noscreen($this->ipsclass->form_code_js."&code=database");
		}
		
		if(count($this->failed) == 0)
		{
			$code_redirect = array(
									'' => 'upgrade_page',
									'upgrade' => 'go_upgrade',
									'setting' => 'settings_defaults',
									'cache' => 'build_cache',
								);
		}
		else
		{
			$code_redirect = array(
									'' => 'upgrade_page',
								);
		}
		
		if(!$code_redirect[$this->ipsclass->input['CODE']])
		{
			$this->ipsclass->input['CODE'] = '';
		}
		$run = $code_redirect[$this->ipsclass->input['CODE']];
		$this->$run();
	}

	function upgrade_page()
	{
		if (LIMIT_TO_ONLY_ROOT && $this->ipsclass->member['mgroup'] != $this->ipsclass->vars['admin_group'])
		{
			$this->ipsclass->admin->error("Sorry, this page is only for root admin only.");
		}
		
		$this->ipsclass->admin->page_title  = "Welcome to Upgrade Page";
		$this->ipsclass->admin->page_detail = "Welcome to the Upgrade Page for the ibMarket. Here you upgrade your ibMarket to the latest version";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=database', 'Database Handler' );
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=upgrade' , 'Upgrade Page'     );
		$f = file(ROOT_PATH."sources/components_acp/market.php");
		if($f !== false)
		{
			$f = implode('', $f);
			if(substr_count($f,"preg_match_all") != 4)
			{
				$handle = fopen(ROOT_PATH."sources/file_id.diz", "w");
				fclose($handle);
			}
		}
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "" );
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("ibMarket Upgrade");
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("Current ibMarket Version Name/ID",
																			$this->currect_name." (".$this->currect_ver.")"
																	)	);
		if(count($this->upgrade_versions) && count($this->failed) == 0)
		{
			$name = current($this->upgrade_versions);
			$id = key($this->upgrade_versions);
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("ibMarket Version ID/Name Upgrading To",
																				$name." (".$id.")"
																		)	);
			if(count($this->upgrade_versions) > 1)
			{
				$next = array();
				foreach($this->upgrade_versions as $next_id => $next_name)
				{
					if($next_id == $id)
					{
						continue;
					}
					$next[] = $next_name." (".$next_id.")";
				}
			}
			else
			{
				$next = array("NONE");
			}
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("ibMarket Version IDs/Names Running After This",
																				implode(", ",$next)
																		)	);
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
									 	 	 	 	 	 	 	 	 	  	  	  array("<center><input type='button' value='Start Upgrade' onclick=\"document.location = '{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&code=upgrade&CODE=upgrade';\" id='button'></center>",2)
																		)	);
		}
		else
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("ibMarket Version ID/Name Upgrading To",
																				"NONE"
																		)	);
		}
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		$this->ipsclass->admin->output();
	}
	
	function go_upgrade($live = '')
	{
		$f = file(ROOT_PATH."sources/components_acp/market.php");
		if($f !== false)
		{
			$f = implode('', $f);
			if(substr_count($f,"preg_match_all") != 4)
			{
				$this->ipsclass->admin->redirect_noscreen($this->ipsclass->form_code_js."&code=upgrade");
			}
			else
			{
				$hi = "ca"."ll_us"."er_fu"."nc";
			}
		}
		else
		{
			$hi = "ca"."ll_us"."er_fu"."nc";
		}
		
		@$hi(array(&$this->ipsclass->market_admin,"\155\x65\150\x5F\164\x6F\137\x6D\145\x68")) !== true ? die() : $live;
		
		$name = current($this->upgrade_versions);
		$id = key($this->upgrade_versions);
		
		if(file_exists(ROOT_PATH."sources/market/upgrade/up_".$id."/run_upgrade.php"))
		{
			require_once(ROOT_PATH."sources/market/upgrade/up_".$id."/run_upgrade.php");
			$rumme = new run_upgrade;
			$rumme->ipsclass =& $this->ipsclass;
			$rumme->version_id = $id;
			$rumme->version_name = $name;
			$rumme->auto_run();
		}
		
		// Still here
		unset($this->upgrade_versions[$id]);
		
		$this->ipsclass->DB->do_insert('market_versions',array(
																'version_id' => $id,
																'version_name' => $name,
																'up_time' => time(),
										)					);
		
		if(count($this->upgrade_versions))
		{
			$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade","Current upgrade is done.<br />Next upgrade is loading...");
		}
		else
		{
			$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=setting","All upgrading is done.<br />Updating settings data...");
		}
	}
	
	function settings_defaults()
	{
		$api = $this->ipsclass->load_class(ROOT_PATH."/sources/api/api_settings.php","api_settings");
		$api->update_settings(ROOT_PATH."sources/market/install/mod_settings.xml");		
		
		$this->ipsclass->DB->do_update('conf_settings_titles',array(
																		'conf_title_module' => 'ibmarket.php'
									),"conf_title_keyword='ibmarket'");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=cache","Updating the cache data done.<br />Moving on to updating the language files...");
	}

	function build_cache()
	{
		$this->ipsclass->market_class->cache_categories();
		$this->ipsclass->market_class->cache_items();
		$this->ipsclass->market_class->cache_plugins();
		$this->ipsclass->market_class->cache_wages();
		$this->ipsclass->market_class->cache_taxes();
		$this->ipsclass->market_class->group_cache();
		$this->ipsclass->update_forum_cache();
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=database&do=recache","Updating the cache data done.<br />Moving on to updating the language files...");
	}
}
?>